package com.ajx.blog.controller;

import com.ajx.blog.entity.Admin;
import com.ajx.blog.entity.Menu;
import com.ajx.blog.entity.Roles;
import com.ajx.blog.service.api.MenuService;
import com.ajx.blog.service.api.RolesService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/blog/all")
public class InitController {

    private Logger logger = LoggerFactory.getLogger(InitController.class);

    @Reference(url = "dubbo://localhost:20880")
    private RolesService rolesService;

    @Reference(url = "dubbo://localhost:20880")
    private MenuService menuService;

    /**
     * @param {HttpSession} session 作用域
     * @return {Object}
     * 
     * @description: 跳转到main页面
     */
    @RequestMapping("/jumpMainView")
    public String jumpMainView(HttpSession session) {
        Admin principal = (Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        session.setAttribute("loginAdmin", principal);
        logger.info("将登录管理员{}存储到session中", principal);
        return "redirect:/main.html";
    }

    /**
     * @param {HttpSession} session 作用域
     * @return {Object}
     * 
     * @description: 初始化页面上方top.html,加载登录管理员姓名
     */
    @RequestMapping("/initTop")
    @ResponseBody
    public Object initTop(HttpSession session) {
        Map<String, Object> returnMap = new HashMap<>();
        if (session.getAttribute("loginAdmin") == null) {
            returnMap.put("code", -1);
            returnMap.put("data", null);
            returnMap.put("msg", "初始化管理员页面上方top.html失败,原因:未登录");
            logger.info("初始化管理员页面上方top.html失败,原因:未登录");
            return returnMap;
        } else {
            Admin admin = (Admin) session.getAttribute("loginAdmin");
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("admin", admin.getAdminNickname());
            returnMap.put("code", 0);
            returnMap.put("data", dataMap);
            returnMap.put("msg", "初始化Top成功");
            logger.info("初始化管理员页面上方top.html成功,登录管理员用户:{}", admin);
            return returnMap;

        }
    }

    /**
     * @param {HttpSession} session 作用域
     * @return {Object}
     * 
     * @description: 加载左侧选项卡,根据角色动态变化
     */
    @RequestMapping("/initLeft")
    @ResponseBody
    public Object initLeft(HttpSession session) {
        Map<String, Object> returnMap = new HashMap<>();
        if (session.getAttribute("loginAdmin") == null) {
            returnMap.put("code", -1);
            returnMap.put("data", null);
            returnMap.put("msg", "加载管理员左侧选项卡失败,原因:用户未登录");
            logger.info("加载管理员左侧选项卡失败,原因:用户未登录");
            return returnMap;
        } else {
            Admin admin = (Admin) session.getAttribute("loginAdmin");
            // 当前登录用户的角色
            List<Roles> rolesList = rolesService.selectRoleByAdmin(admin.getAdminId());
            Map<String, Object> pmap = new HashMap<>();
            Map<Integer, List<Menu>> secondmenumap = new HashMap<>();
            pmap.put("roleList", rolesList);
            // 一级菜单
            List<Menu> firstmenuList = menuService.selectMenuByMap(pmap);

            for (Menu firstmenu : firstmenuList) {
                pmap.put("parentId", firstmenu.getId());
                // 二级菜单
                List<Menu> secondmenuList = menuService.selectMenuByMap(pmap);
                if (secondmenuList.size() > 0) {
                    secondmenumap.put(firstmenu.getId(), secondmenuList);
                }
            }

            logger.info("登录管理员用户的角色列表:{}", rolesList);
            logger.info("一级菜单列表:{}", firstmenuList);
            logger.info("二级菜单列表:{}", secondmenumap);

            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("admin", admin);
            dataMap.put("rolesList", rolesList);
            dataMap.put("firstmenuList", firstmenuList);
            dataMap.put("secondmenumap", secondmenumap);

            returnMap.put("code", 0);
            returnMap.put("data", dataMap);
            returnMap.put("msg", "加载管理员左侧选项卡成功");
            logger.info("加载管理员左侧选项卡成功");

            return returnMap;
        }
    }

}
